// pages/invitation_poster/invitation_poster.js
var api = require('../../api/api');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    images:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getPoster();
  },
  getPoster(){
    api.get('/user/poster').then(res=>{
      this.setData({
        images:res.poster
      })
    })
  },
  cImage(){
    const query = wx.createSelectorQuery()
    query.select('#myCanvas')
      .fields({ node: true, size: true })
      .exec((res) => {
        const canvas = res[0].node
        const ctx = canvas.getContext('2d')
        const dpr = wx.getSystemInfoSync().pixelRatio
        canvas.width = res[0].width * dpr
        canvas.height = res[0].height * dpr
        ctx.scale(dpr, dpr)

        //画背景图
           //写入海报背景
           const poster = new Promise((resove, reject) => {
            const posterBgImg = canvas.createImage();
            const poster_bg = '/images/yaoqing_bg.png';
            posterBgImg.src = poster_bg;
            posterBgImg.onload = () => {
              //img.complete表示图片是否加载完成，结果返回true和false;
              ctx.drawImage(posterBgImg,0, 0, 270, 479);
              this.setData({
                canvasW:res[0].width,
                canvasH:res[0].height 
              })
              console.log("第一")
              resove(ctx)
            };
          }).then(res=>{
            //画二维码
            const qrcode = new Promise((resolve, reject) => {
              const qrcodeImg = canvas.createImage();
              const qrcode ="/images/eq_code.png";
              qrcodeImg.src = qrcode;
              qrcodeImg.onload = () => {
                //img.complete表示图片是否加载完成，结果返回true和false;
                ctx.drawImage(qrcodeImg, (270- 120) / 2, 320, 120, 120);
               
                console.log("第二")
                resolve(ctx)
              };
            }).then(res=>[
              wx.canvasToTempFilePath({
                width:canvas.width,
                height:canvas.height,
                destWidth:canvas.width*dpr,
                destHeight:canvas.height*dpr,
                canvas:canvas,
                fileType: 'png',
                success:res=>{
                  console.log(res.tempFilePath)
                  const poster_path=res.tempFilePath;
                  this.data.poster_path=poster_path;
                  wx.hideLoading({
                    success: (res) => {},
                  })
                  this.setData({
                    show_save:true
                  })
                }
              })
            ])

          })




        // ctx.fillRect(0, 0, 100, 100)
      })
  },
  click_save_image(){
    const poster_path=this.data.images;
    wx.getImageInfo({
      src: poster_path,
      success: res => {
        var poster_image = res.path;
        console.log(poster_image)
        wx.saveImageToPhotosAlbum({
          filePath: poster_image,//临时路径或者本地路径不支持网络路径
          success: res => {
            console.log("保存成功")
          },
          fail:err=>{
            console.log("失败",err)
            wx.getSetting({
             success:res=>{
               console.log(res)
               if(res.authSetting['scope.writePhotosAlbum']==false){
                 //还没开启权限
                 console.log("没权限")
                 wx.showToast({
                   title: '请在设置打开添加到相册权限',
                   icon:"none"
                 })
               }
             }
            })
          }
        })
      }
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})